<?php
	require_once("../../../include/connect.php");
	require_once("../../../include/function.php");
	require_once("../../../include/datetime.php");
	$nam = $_POST['nam'];
	$date = day_now();
	$query = mysql_query(" SELECT id,subject,num_day,day FROM schedule WHERE year = {$nam} ORDER BY num_day ");
	if(mysql_num_rows($query) > 0)
	{
		while($row = mysql_fetch_array($query))
		{
			$id = $row['id'];
			$subject = $row['subject'];
			$num_day = $row['num_day'];
			$day = $row['day'];
			$arr = explode(",",$day);
			$cnt = count($arr);
			
			$k=1; $i=0;
			while($i<$cnt && $k<=$num_day)
			{
				$str = substr($arr[$i],0,1);
				$buoi = check_buoi($str);
				$thu = substr($arr[$i],1);
				
//					echo $thu."_".$buoi."_".$nam;
					
				$q = mysql_query(" SELECT mon FROM action_schedule WHERE thu = '{$thu}' AND buoi = '{$buoi}' AND nam = '{$nam}' ");	
				if(mysql_num_rows($q) > 0)
				{
					$r = mysql_fetch_array($q);
					if(check_group($r['mon'],$subject) == 1)
						continue;
				}
				$q1 = mysql_query(" INSERT INTO action_schedule(nam,thu,buoi,mon,date) 
									VALUES('{$nam}', '{$thu}', '{$buoi}', '{$subject}', '{$date}'  ) ");
				if($q1)
					$s = true;
				else
					$s = false;
				$k++;$i++;
			}
		}
		
		if($q1)
		{
			/*$query1 = mysql_query(" SELECT mon,thu,buoi FROM action_schedule WHERE nam = '{$nam}' ");
			if(mysql_num_rows($query1) > 0)
			{
				while($row1 = mysql_fetch_array($query1))
				{
					echo "<tr>";
						echo "<td> ".$row1['thu']." </td>";
						echo "<td> ".$row1['buoi']." </td>";
						echo "<td> ".$row1['mon']." </td>";
					echo "</tr>";	
				}	
			}*/
			echo "a";
		}
		
		
			
	}	
?>